<?php
include_once ('../global.php');
    $base64_image = $_POST['image'];
$user_id = $_POST['user_id'];
$type = $_POST['type'];
$mt_id=4579;//比赛id
//判断是否提交
$sql = "SELECT * FROM `bmw_member_sign` WHERE user_id = '".$user_id."' and type='".$type."' and mt_id='{$mt_id}'";
$res = $db->SelectSQL($sql);
if($res){
    $response = array(
        'code' => 0,
        'msg' => '已提交过签名'
    );
    echo json_encode($response);
    exit;
}
    // 转换Base64编码为图片文件并保存
    $image_service_path = 'uploads/'; // 确保这个目录有写权限
    $image_name = uniqid() . '.png'; // 或者根据需要设定文件名和扩展名
    $image_full_path = $image_service_path . $image_name;

    $image_data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64_image));
    if (file_put_contents($image_full_path, $image_data)) {
        $response = array(
            'code' => 1,
            'msg' => '文件上传成功',
            'image_path' => $image_full_path
        );
        //插入数据库
        $act_datetime=date("Y-m-d H:i:s");
        //入库
        $sql=" insert into bmw_member_sign(user_id,type,sign_img,create_time,mt_id) values($user_id,'{$type}','{$image_name}','{$act_datetime}',{$mt_id});";
        $db->ExecuteSQL($sql);
    } else {
        $response = array(
            'code' => 0,
            'msg' => '签名上传失败'
        );
    }

    header('Content-Type: application/json');
    echo json_encode($response);

?>